package com.samp.util;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class FilesUtils {

    public static void saveListToFile(List<String> list, String rawFile) throws IOException {
        saveListToFile(list,rawFile,null);
    }

    public static void saveListToFile(List<String> list, String rawFile, String split) throws IOException {
        BufferedWriter writer = null;
        try {
            writer = Files.newBufferedWriter(Paths.get(rawFile), StandardCharsets.UTF_8);
            for( String rig : list ){
                if( StringUtils.isBlank(rig) ){
                    continue;
                }
                writer.write(rig);
                if(split == null) {
                    writer.newLine();
                }else {
                    writer.write(split);
                }
            }
            writer.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if( writer != null ){
                writer.close();
            }
        }
    }

    public static void saveListsToFile(List<List<String>> lists, String rawFile, String split) throws IOException {
        BufferedWriter writer = null;
        try {
            writer = Files.newBufferedWriter(Paths.get(rawFile), StandardCharsets.UTF_8);
            for(List<String> list:lists ) {
                for ( int i = 0 ; i< list.size(); i++) {
                    String rig = list.get(i);
                    if (StringUtils.isBlank(rig)) {
                        continue;
                    }
                    writer.write(rig);
                    if( i != list.size() -1 ) {
                        writer.write(split);
                    }
                }
                writer.newLine();
            }
            writer.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if( writer != null ){
                writer.close();
            }
        }
    }

    public static String getTextFromPath(String path){
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader bf = Files.newBufferedReader(Paths.get(path));
            String line = bf.readLine();
            while( line != null ){
                if( StringUtils.isNotBlank(line) ){
                    sb.append(line).append("\n");
                }
                line = bf.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 按行读取。
     * @param path
     * @return
     */
    public static List<String> getTextsFromPath(String path){
        List<String> result = new ArrayList<>();
        try {
            BufferedReader bf = Files.newBufferedReader(Paths.get(path));
            String line = bf.readLine();
            while( line != null ){
                if( StringUtils.isNotBlank(line) ) {
                    if( !line.startsWith("#")){
                        result.add(line);
                    }
                }
                line = bf.readLine();
            }
            bf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static List<String> getFilesFromDir(String dirPath) throws Exception{
        Path dir = Paths.get(dirPath);
        List<String> list = new ArrayList<>();
        try( DirectoryStream<Path> stream = Files.newDirectoryStream(dir) ){
            for(Path path : stream){
                if( !Files.isDirectory(path) ){
                    list.add(path.getFileName().toString());
                }
            }
            return list;
        }catch(IOException e){
            throw new Exception(dirPath + " read error. ", e) ;
        }
    }

}
